let optionInfoModel = require("../../model/blog/optionTable");
let Service = {
    //查询
    query: (info) => optionInfoModel.find(info).sort({ top: -1 }),
    //多条件查询（字典）info:[]
    queryOr: (info) => {
        const convertedValues = info.map((value) => {
            return { type: String(value) };
        });
        const query = { $or: convertedValues };
        return optionInfoModel.find(query).sort({ top: -1 });
    },
    //根数据操作
    // 添加
    add: (info) => optionInfoModel.insertMany([info]),
    //删除
    delete: (info) => optionInfoModel.deleteOne({ _id: info._id }),
    //修改
    update: ({ _id, name, type, remark, updateTime }) => optionInfoModel.updateOne({ _id }, { $set: { name, type, remark, updateTime } }),

    //values[]操作
    //添加
    addValues: (info) => optionInfoModel.updateOne({ _id: info._id }, { $push: { values: { label: info.label, value: info.value, remark: info.remark, updateTime: info.updateTime, createTime: info.createTime } } }),
    //删除
    deleteValues: (info) => optionInfoModel.updateOne({ _id: info._id }, { $pull: { values: { _id: info.vid } } }),
    //修改
    updateValues: (info) =>
        optionInfoModel.updateOne(
            { _id: info._id, "values._id": info.vid },
            {
                $set: {
                    "values.$.label": info.label,
                    "values.$.value": info.value,
                    "values.$.remark": info.remark,
                    "values.$.updateTime": info.updateTime,
                },
            }
        ),
};
module.exports = Service;
